! function() {
	/**
	 * 分页参数初始值
	 * 1.current:当前页(默认第1页)
	 * 2.size:每页显示的记录条数(默认12条)
	 * 3.totalPage:总页数(默认1)
	 * 4.loading:是否正在加载数据(true:是;false:否)
	 */

	const page = {
		current: 1,
		size: 8,
		totalPage: 1,
		loading: false
	};


	/**
	 * 检索视频
	 * @param keyword 搜索关键字
	 * @param categoryId 视频分类ID
	 * @param restart 是否从第一页开始查询,当条件改变后从第1页开始查询,当条件没有改变,查询下一页数据
	 */
	async function searchVideo(keyword, categoryId, restart = false) {
		if (page.loading) {
			return;
		}
		page.loading = true;
		if (restart) {
			page.current = 1;
			$('.video-list').empty(); //
		}
		//查询条件
		let params = {
			current: page.current,
			size: page.size
		}
		if (keyword) {
			params.keyword = keyword;
		}
		if (categoryId) {
			params.categoryId = categoryId;
		}

		let responseData = await getResponse(apis.video.getPageList, 'get', params);
		page.loading = false;
		if (200 !== responseData.code) {
			alert(`查询视频数据失败:${responseData.message}`);
			return;
		}
		let data = responseData.data;
		page.totalPage = data.totalPage;
		let videoList = data.records;
		if ($.isEmptyObject(videoList)) {
			$('.video-list').append('<div  style="text-align: center;color: orange;">未查询到任何视频数据</div>');
			return;
		}
		//加载视频数据
		videoList.forEach(video => {
			$('.video-list').append(
				`<div class="video-item"><div class="video-thumbnail" style="background-image: url('https://picsum.photos/id/96/600/400');"></div><div class="video-title">${video.title}</div></div>`
			);
		});
	}

	/**
	 * 重置分页参数
	 */
	function resetPage() {
		page = {
			current: 1,
			size: 12,
			totalPage: 1,
			loading: false
		};
	}

	/**
	 * 当滚动条到达页面底部某位置处,自动加载下一页的数据
	 */
	$(window).scroll(function() {
		if (page.loading) {
			return;
		}
		//当前网页高度
		let dh = $(document).height();
		//当前滚动条高度
		let st = $('html, body').scrollTop() + window.innerHeight + 2;
		//页面卷去高度 + 浏览器可视窗口的高度 >= 整个页面的高度
		if (st >= dh) {
			if (page.current === page.totalPage) {
				return;
			}
			let nextPage = page.current + 1;
			page.current = nextPage;
			let keyword = $('#keyword').val(); //关键字
			let categoryId = $('.video-categories>.active').data('category-id');
			console.info(categoryId)
			searchVideo(keyword, categoryId, false);
		}
	});
	$( async function() {
		//当网页加载完毕后执行
		$('.search').click(function() {
			let keyword = $('#keyword').val(); //关键字
			let categoryId = $('.video-categories>.active').data('category-id');
			searchVideo(keyword, categoryId, true);
		});
		$('.search').click();
		//当网页加载完毕执行--加载视频分类
		let responseData = await getResponse(apis.video.category.list);
		if (200 !== responseData.code) {
			alert(`加载视频分类数据失败`);
			return;
		}
		let categoryList = responseData.data;
		if ($.isEmptyObject(categoryList)) {
			alert('未加载到任何视频分类数据');
			return;
		}
		categoryList.forEach(category => {
			$('.video-categories').append(
				`<button data-category-id="${category.videoCategoryId}" class="category-btn">${category.name}</button>`
			);
		});
		//给所有分类Button加上click事件
		$('.category-btn').each(function(i) {
			$(this).click(function() {
				//移除上一次选中的视频分类button标签的active样式
				$('.video-categories>.active').removeClass('active');
				//将当前视频分类按钮选中,即加上active
				$(this).addClass('active');
				let keyword = $('#keyword').val(); //关键字
				let categoryId = $(this).data('category-id');
				searchVideo(keyword, categoryId, true);
			});
		});
	});
}();